﻿/// 浏览器相关操作
var KR_browse = {

    /*	设为首页
    @url		要设为首页的地址
    */
    SetDefault: function () {
        this.style.behavior = 'url(#default#homepage)';
        this.setHomePage(this.GetUrl());
        return false;
    },

    /*	复制指定URL地址
    @Msg		要写入剪贴板的字符集
    */
    SetCopy: function (Msg) {
        if (navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
            clipboardData.setData('Text', Msg);
            alert("网址“" + Msg + "”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
        }
        else {
            prompt("请复制网站地址:", Msg);
        }
    },

    /*	加入收藏
    @site		站点名称
    @url		地址
    */
    AddBookmark: function (site, url) {
        if (navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
            window.external.addFavorite(url, site)
        } else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
            alert("请使用Ctrl+T将本页加入收藏夹");
        } else {
            alert("请使用Ctrl+D将本页加入收藏夹");
        }
    },

    /*	打开Url指定宽度和高度的窗口 */
    OpenWindows: function (url, width, height, newWindowId) {
        if (newWindowId == null) newWindowId = "newwin";

        var para = 'resizable=no,width=' + width + ',height=' + height + ',left=' + (screen.width - parseInt(width)) / 2 + ',top=' + (screen.height - height - 20) / 2;
        window.open(url, newWindowId, para);
        return false;
    },

    /*	打开Url指定宽度和高度的窗口 */
    OpenModalDialog: function (url, width, height, newWindowId) {
        if (newWindowId == null) newWindowId = "newModalDialog";

        var para = "resizable=no; toolbar:no; status:no; dialogWidth:" + width + "px; dialogHeight:" + height + "px;";
        return showModalDialog(url, newWindowId, para);
    },

    /*	禁止浏览器的Javascript错误提示 */
    CloseError: function () {
        window.onerror = function () { return true; };
    },

    /*	获取浏览器URL */
    GetUrl: function () {
        return location.href;
    },

    /*	获取URL参数 */
    GetUrlParam: function () {
        return location.search;
    },

    /*	获取页面来源 */
    GetFrom: function () {
        return document.referrer;
    },

    /*	获取指定的URL参数值
    @name		参数名
    */
    Request: function (name) {
        var GetUrl = this.GetUrl();
        var Plist = new Array();
        if (GetUrl.indexOf('?') > 0) {
            Plist = GetUrl.split('?')[1].split('&');
        }
        else if (GetUrl.indexOf('#') > 0) {
            Plist = GetUrl.split('#')[1].split('&');
        }
        if (GetUrl.length > 0) {
            for (var i = 0; i < Plist.length; i++) {
                var GetValue = Plist[i].split('=');
                if (GetValue[0].toUpperCase() == name.toUpperCase()) {
                    return GetValue[1];
                    break;
                }
            }
            return;
        }
    },

    /*	直接将HTML写到新窗口
    @title		标题
    @msg		内容
    */
    Popmsg: function PopIt(title, msg) {
        var popup = window.open('', 'popDialog', 'height=500,width=400,scrollbars=yes');
        popup.document.write('<html><title>' + title + '</title><style>body{margin:10px;font:13px Arial;}span{text-line:20px;}</style><body><span style=\'font:14px arial;\'>' + msg + '</span></body></html>');
        popup.document.close();
    }
};


/// 对象操作
var KR_object = {
    /*	拷贝对象值到剪贴板  
    @str		对象值
    */
    CopyCode: function (str) {
        var rng = document.body.createTextRange();
        rng.moveToElementText(str);
        rng.scrollIntoView();
        rng.select();
        rng.execCommand("Copy");
        rng.collapse(false);
    },

    /*	当前对象是否可见
    @objid		对象ID
    */
    IsVisible: function (objid) {
        if (this.Get(objid)) {
            try {
                if (this.Get(objid).style.display == 'none') {
                    return false
                }
                if (this.Get(objid).style.visibility == 'hidden') {
                    return false;
                }
                return true;
            }
            catch (e) {
                return false;
            }
        }
        else {
            return false;
        }
    }
};

/// 字符处理
var KR_string = {
    /*	取左边的指定长度的值  
    @str		要处理的字符集
    @n			长度
    */
    Left: function (str, n) {
        if (str.length > 0) {
            if (n > str.length) n = str.length;
            return str.substr(0, n)
        }
        else {
            return;
        }
    },

    /*	取右边的指定长度的值   
    @str		要处理的字符集
    @n			长度
    */
    Right: function (str, n) {
        if (str.length > 0) {
            if (n >= str.length) return str;
            return str.substr(str.length - n, n);
        }
        else {
            return;
        }
    },

    /*	Trim:清除两边空格 
    @str		要处理的字符集
    */
    Trim: function (str) {
        if (typeof str == 'string') return str.replace(/(^\s*)|(\s*$)/g, '');
    },

    /*	LTrim:清除左边的空格 
    @str		要处理的字符集
    */
    Ltrim: function (str) {
        if (typeof str == 'string') return str.replace(/(^\s*)/g, '');
    },

    /*	RTrim: 清除右边的空格 
    @str		要处理的字符集
    */
    Rtrim: function (str) {
        if (typeof str == 'string') return str.replace(/(\s*$)/g, '');
    },

    /*	清除前后的非字符
    @str		要处理的字符集
    */
    strip: function (str) {
        if (typeof str == 'string') return str.replace(/^\s+/, '').replace(/(^\s*)|(\s*$)/g, '');
    },

    /*	过滤字符里面的HTML标签
    @str		要处理的字符集
    */
    stripTags: function (str) {
        if (typeof str == 'string') return str.replace(/<\/?[^>]+>/gi, '').replace(/(^\s*)|(\s*$)/g, '');
    }
};

/// 时间相关操作
var KR_time = {
    /*	获取当天日期 yyyy-MM-dd */
    GetDateNow: function () {
        var d, y, m, dd;
        d = new Date();
        y = d.getYear();
        m = d.getMonth() + 1;
        dd = d.getDate();
        return y + "-" + m + "-" + dd;
    },

    /* 转换字符串*/
    ToString: function (date) {
        yy = date.getFullYear();
        mm = date.getMonth() + 1;
        dd = date.getDate();
        hours = date.getHours();
        minutes = date.getMinutes();
        seconds = date.getSeconds();
        datestr = yy + '-' + mm + '-' + dd + ' ' + hours + ':' + minutes + ':' + seconds;
        return datestr
    },

    /*	获取指定日期后的特定天数的日期值
    @toDate		当前指定的日期
    @N			要添加的日期数
    */
    AddDays: function (toDate, N) {
        var aDate = this._cvtISOToDate(toDate);
        if (!aDate) return "";
        var millis = 86400000 * N;
        aDate = new Date(aDate.getTime() + millis);
        return this._fmtDateISO(aDate);
    },
    _fmtDateISO: function (aDate) {
        with (aDate) {
            var mm = getMonth() + 1;
            if (mm < 10) { mm = '0' + mm; }
            var dd = getDate();
            if (dd < 10) { dd = '0' + dd; }
            return (getFullYear() + '-' + mm + '-' + dd);
        }
    },
    _cvtISOToDate: function (isoDate) {
        var atomDate = isoDate.split('-');
        var aDate = new Date(parseInt(atomDate[0], 10), parseInt(atomDate[1], 10) - 1, parseInt(atomDate[2], 10), 6, 0, 0);
        return aDate;
    }
};

/// 科学计算
var KR_account = {
    /*	逢1进10计算
    @ 数值
    */
    GetTen: function (i) {
        var items_One, Get_One;
        if (i.length > 1 && (/^\d+$/.test(i))) {
            items_One = i.substr(0, i.length - 1);
            Get_One = i.substr(i.length - 1, 1);
            if (parseInt(Get_One) > 0) {
                items_One = parseInt(items_One) + 1;
                items_One = items_One + '0';
            }
            else {
                items_One = items_One + '0';
            }
        }
        else {
            items_One = i;
        }
        return items_One;
    },
    /* 四舍五入
    *
    */
    Round: function (a, b) { return parseFloat(parseFloat(a).toFixed(b)); },
    /*
    取N位小数
    src为原数字，pos为保留几位小数
    */
    formatFloat: function (src, pos) {
        return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
    },
    //浮点数加法运算 
    //ThinkWang 2013-1-22 
    floatAdd: function (arg1, arg2) {
        var r1, r2, m;
        try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
        try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
        m = Math.pow(10, Math.max(r1, r2))
        return ((KR_account.floatMul(arg1, m) + KR_account.floatMul(arg2, m)) / m);
    },

    //浮点数减法运算 
    //ThinkWang 2013-1-22 
    floatSub: function (arg1, arg2) {
        var r1, r2, m, n;
        try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
        try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
        m = Math.pow(10, Math.max(r1, r2));
        //动态控制精度长度  
        n = (r1 >= r2) ? r1 : r2;
        return ((KR_account.floatMul(arg1, m) - KR_account.floatMul(arg2, m)) / m).toFixed(n);
    },

    //浮点数乘法运算  
    //ThinkWang 2013-1-22
    floatMul: function (arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try { m += s1.split(".")[1].length } catch (e) { }
        try { m += s2.split(".")[1].length } catch (e) { }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    },


    //浮点数除法运算
    //ThinkWang 2013-1-22  
    floatDiv: function (arg1, arg2) {
        var t1 = 0, t2 = 0, r1, r2;
        try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
        try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
        with (Math) {
            r1 = Number(arg1.toString().replace(".", ""))
            r2 = Number(arg2.toString().replace(".", ""))
            return KR_account.floatMul((r1 / r2), pow(10, t2 - t1));
        }
    }
};

//可操作的Map类
function Map() {
    var struct = function (key, value) {
        this.key = key;
        this.value = value;
    }

    var put = function (key, value) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                this.arr[i].value = value;
                return;
            }
        }
        this.arr[this.arr.length] = new struct(key, value);
    }

    var merge = function (key, value, split) {
        if (this.get(key) == null) {
            this.put(key, value);
        }
        else {
            var tmp = this.get(key);
            this.remove(key);
            if (split == null) split = "";
            this.put(key, tmp + split + value);
        }
    }

    var get = function (key) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                return this.arr[i].value;
            }
        }
        return null;
    }

    var remove = function (key) {
        var v;
        for (var i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if (v.key === key) {
                continue;
            }
            this.arr.unshift(v);
        }
    }

    var size = function () {
        return this.arr.length;
    }

    var isEmpty = function () {
        return this.arr.length <= 0;
    }

    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.merge = merge;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
}

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(), //day 
        "h+": this.getHours(), //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
        "S": this.getMilliseconds() //millisecond 
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
//重写数值的保留小数位的方法
Number.prototype.toFixed = function (exponent) {
    return parseInt(this * Math.pow(10, exponent) + 0.5) / Math.pow(10, exponent);
}
// JavaScript Document
function switchSysBar() {
    var obj = document.getElementById("switchPoint");
    if (obj.alt == "open") {
        obj.alt = "close";
        obj.src = "/Content/images/pages/L.jpg"; //此处为关闭状态图片
        document.getElementById("leftHiddenTd").style.display = "none"; //leftHiddenTd为左侧需隐藏的区域元素ID
    } else {
        obj.alt = "open";
        obj.src = "/Content/images/pages/L.jpg"; //此处图片为打开状态图片
        document.getElementById("leftHiddenTd").style.display = "";
    }
}

var intLeft = 2;
function returnPage(Url) {
    if (0 == intLeft) {
        window.location.href = Url;
    }
    else {
        intLeft -= 1;
        setTimeout("returnPage('" + Url + "')", 2000);
    }
}

function showlayer(layer) {
    //导航菜单
    var myLayer = document.getElementById(layer);
    if (myLayer.style.display == "none" || myLayer.style.display == "") {
        myLayer.style.display = "block";
    } else {
        myLayer.style.display = "none";
    }
}